<template>
  <div>
    <div class="head-container">
      <el-row style="margin-bottom: 10px">
        <el-col :span="5" style="margin-right: 10px">
          <el-input v-model="query.scriptName" clearable placeholder="请输入脚本名称" />
        </el-col>
        <rrOperation :crud="crud" />
        <el-button class="filter-item" type="info" icon="el-icon-s-data" size="mini" @click="toDataAnalysis">数据分析</el-button>
        <el-button class="filter-item" type="info" icon="el-icon-s-data" size="mini" @click="toDataSynchronize">数据同步</el-button>
      </el-row>
    </div>
    <el-table
      ref="table"
      :key="itemKey"
      v-loading="crud.loading"
      border
      max-height="270"
      :data="crud.data"
      row-key="pkId"
      @filter-change="crud.handleFilterChange"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="scriptName" label="脚本名称" />
      <el-table-column prop="sourceTableNameList" label="源表">
        <template slot-scope="scope">
          <el-tag
            v-for="sourceTableName in scope.row.sourceTableNameList"
            :key="sourceTableName"
            closable
            :disable-transitions="false"
            @close="sourceTableNamehandleClose(scope.row, sourceTableName)"
          >
            {{ sourceTableName }}
          </el-tag>
          <el-input
            v-if="scope.row.newSourceTableNameVisible"
            ref="addSourceTableName"
            v-model="newSourceTableName"
            class="input-new-tag"
            size="small"
            @keyup.enter.native="handleSourceTableInputConfirm(scope.row)"
            @blur="handleSourceTableInputConfirm(scope.row)"
          />
          <el-button v-else class="button-new-tag" size="small" @click="showSourceTableInput(scope.row)">+ Add Table</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="targetTableNames" label="目标表">
        <template slot-scope="scope">
          <el-tag
            v-for="targetTableName in scope.row.targetTableNameList"
            :key="targetTableName"
            closable
            :disable-transitions="false"
            @close="targetTableNamehandleClose(scope.row, targetTableName)"
          >
            {{ targetTableName }}
          </el-tag>
          <el-input
            v-if="scope.row.newTargetTableNameVisible"
            ref="addTargetTableName"
            v-model="newTargetTableName"
            class="input-new-tag"
            size="small"
            @keyup.enter.native="handleTargetTableInputConfirm(scope.row)"
            @blur="handleTargetTableInputConfirm(scope.row)"
          />
          <el-button v-else class="button-new-tag" size="small" @click="showTargetTableInput(scope.row)">+ Add Table</el-button>
        </template>
      </el-table-column>
      <el-table-column label="分析结果" width="80">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="dataAnalysisResult(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination />
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="cancelCU"
      :visible.sync="dataAnalysisResultFlag"
      title="脚本数据分析结果"
      width="1000px"
    >
      <data-analysis-result :test-script-list-pk-id="testScriptListPkId" :test-script-task-pk-id="testScriptTaskPkId" />
    </el-dialog>
  </div>
</template>
<script>
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'
import scriptTableConsanguinityApi from '@/api/script/common/scriptTableConsanguinity'
import dataAnalysisResult from '../dataAnalysisResult'
import scriptDataTestTaskApi from '@/api/script/test/scriptDataTestTask'

const defaultCrud = CRUD({
  title: '脚本测试表血缘',
  url: '/api/script/data/test/list/queryConsanguinityList',
  crudMethod: { ...scriptTableConsanguinityApi }
})
const defaultForm = {
  pkId: null
}
export default {
  name: 'ScriptConvertList',
  components: { pagination, rrOperation, dataAnalysisResult },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  props: {
    taskPkId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      queryName: null,
      newSourceTableName: null,
      newTargetTableName: null,
      testScriptListPkId: null,
      testScriptTaskPkId: null,
      dataAnalysisResultFlag: false,
      itemKey: 0
    }
  },
  created() {
  },
  methods: {
    [CRUD.HOOK.beforeRefresh](crud) {
      crud.query.taskPkId = this.$props.taskPkId
    },
    [CRUD.HOOK.afterRefresh](crud) {
      crud.data.forEach(item => {
        item.newSourceTableNameVisible = false
        item.newTargetTableNameVisible = false
      })
    },
    sourceTableNamehandleClose(row, sourceTableName) {
      const dataForm = {
        taskPkId: row.taskPkId,
        scriptListPkId: row.pkId,
        scriptName: row.scriptName,
        tableName: sourceTableName,
        tableType: 'SOURCE',
        dataFrom: 'SCRIPT_TEST'
      }
      scriptTableConsanguinityApi.del(dataForm).then(res => {
        this.crud.notify('删除成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.refresh()
      })
    },
    targetTableNamehandleClose(row, targetTableName) {
      const dataForm = {
        taskPkId: row.taskPkId,
        scriptListPkId: row.pkId,
        scriptName: row.scriptName,
        tableName: targetTableName,
        tableType: 'TARGET',
        dataFrom: 'SCRIPT_TEST'
      }
      scriptTableConsanguinityApi.del(dataForm).then(res => {
        this.crud.notify('删除成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.refresh()
      })
    },
    showSourceTableInput(row) {
      row.newSourceTableNameVisible = true
      this.itemKey = Math.random()
    },
    showTargetTableInput(row) {
      row.newTargetTableNameVisible = true
      this.itemKey = Math.random()
    },
    handleSourceTableInputConfirm(row) {
      if (this.newSourceTableName === null || this.newSourceTableName.trim() === '') {
        this.newSourceTableName = null
        this.crud.refresh()
        return
      }
      const dataForm = {
        taskPkId: row.taskPkId,
        scriptListPkId: row.pkId,
        scriptName: row.scriptName,
        tableName: this.newSourceTableName,
        tableType: 'SOURCE',
        dataFrom: 'SCRIPT_TEST'
      }
      scriptTableConsanguinityApi.add(dataForm).then(res => {
        this.crud.notify('新增成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.newSourceTableName = null
        this.crud.refresh()
      })
    },
    handleTargetTableInputConfirm(row) {
      if (this.newTargetTableName === null || this.newTargetTableName.trim() === '') {
        this.newTargetTableName = null
        this.crud.refresh()
        return
      }
      const dataForm = {
        taskPkId: row.taskPkId,
        scriptListPkId: row.pkId,
        scriptName: row.scriptName,
        tableName: this.newTargetTableName,
        tableType: 'TARGET',
        dataFrom: 'SCRIPT_TEST'
      }
      scriptTableConsanguinityApi.add(dataForm).then(res => {
        this.crud.notify('新增成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.newTargetTableName = null
        this.crud.refresh()
      })
    },
    dataAnalysisResult(row) {
      this.dataAnalysisResultFlag = true
      this.testScriptListPkId = row.pkId
      this.testScriptTaskPkId = row.taskPkId
    },
    cancelCU() {
      this.dataAnalysisResultFlag = false
    },
    // 数据分析
    toDataAnalysis() {
      scriptDataTestTaskApi.dataAlignment(this.taskPkId).then(res => {
        this.crud.notify('数据分析完成！', CRUD.NOTIFICATION_TYPE.SUCCESS)
      })
    },
    // 数据同步
    toDataSynchronize() {
      scriptDataTestTaskApi.dataSynchronize(this.taskPkId).then(res => {
        this.crud.notify('数据同步完成！', CRUD.NOTIFICATION_TYPE.SUCCESS)
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
